home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 9825 / 9825.xpi / components / smarterwiki-search.js < prev   
Text File  |  2009-12-20  |  11KB  |  289 lines

  1. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  2.  
  3. function LOG(msg)
  4. {
  5.     var enabled = false;
  6.     if(enabled)
  7.     {
  8.         var consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
  9.         consoleService.logStringMessage("smarterwiki smarterwiki-search.js: " + msg);
  10.     }
  11. }
  12.  
  13. // Implements nsIAutoCompleteResult
  14. function SearchAutoCompleteResult(result, search_result, maxNumResults) 
  15. {
  16.     this._result = result;
  17.     this._search_result = search_result;
  18.     this._maxNumResults = maxNumResults;
  19.     LOG("SearchAutoCompleteResult: " + result.searchString + " " + result.searchResult + " " + result.defaultIndex + " " + result.errorDescription + " " + result.matchCount);
  20.     //LOG(result.getValueAt(1000));
  21. };
  22.  
  23. SearchAutoCompleteResult.prototype = 
  24. {
  25.     get searchString() 
  26.     {
  27.         return this._result.searchString;
  28.     },
  29.  
  30.     get searchResult()
  31.     {
  32.         LOG("searchResult(): " + this.searchResult2);
  33.         return this.searchResult2;
  34.     },
  35.  
  36.     get searchResult2() 
  37.     {
  38.         switch(this._result.searchResult)
  39.         {
  40.             case Components.interfaces.nsIAutoCompleteResult.RESULT_NOMATCH:
  41.                 if(this._search_result.values == null) {
  42.                     return Components.interfaces.nsIAutoCompleteResult.RESULT_NOMATCH_ONGOING;
  43.                 } else if(this._search_result.values.length > 0) {
  44.                     return Components.interfaces.nsIAutoCompleteResult.RESULT_SUCCESS;
  45.                 } else if(this._search_result.values.length == 0) {
  46.                     return Components.interfaces.nsIAutoCompleteResult.RESULT_NOMATCH;
  47.                 }
  48.                 break;
  49.             case Components.interfaces.nsIAutoCompleteResult.RESULT_SUCCESS:
  50.                 if(this._search_result.values == null) {
  51.                     return Components.interfaces.nsIAutoCompleteResult.RESULT_SUCCESS_ONGOING;
  52.                 } else if(this._search_result.values.length > 0) {
  53.                     return Components.interfaces.nsIAutoCompleteResult.RESULT_SUCCESS;
  54.                 } else if(this._search_result.values.length == 0) {
  55.                     return Components.interfaces.nsIAutoCompleteResult.RESULT_SUCCESS;
  56.                 }
  57.                 break;
  58.             case Components.interfaces.nsIAutoCompleteResult.RESULT_NOMATCH_ONGOING:
  59.                 if(this._search_result.values == null) {
  60.                     return Components.interfaces.nsIAutoCompleteResult.RESULT_NOMATCH_ONGOING;
  61.                 } else if(this._search_result.values.length > 0) {
  62.                     return Components.interfaces.nsIAutoCompleteResult.RESULT_SUCCESS_ONGOING;
  63.                 } else if(this._search_result.values.length == 0) {
  64.                     return Components.interfaces.nsIAutoCompleteResult.RESULT_NOMATCH_ONGOING;
  65.                 }
  66.                 break;
  67.             case Components.interfaces.nsIAutoCompleteResult.RESULT_SUCCESS_ONGOING:
  68.                 if(this._search_result.values == null) {
  69.                     return Components.interfaces.nsIAutoCompleteResult.RESULT_SUCCESS_ONGOING;
  70.                 } else if(this._search_result.values.length > 0) {
  71.                     return Components.interfaces.nsIAutoCompleteResult.RESULT_SUCCESS_ONGOING;
  72.                 } else if(this._search_result.values.length == 0) {
  73.                     return Components.interfaces.nsIAutoCompleteResult.RESULT_SUCCESS_ONGOING;
  74.                 }
  75.                 break;
  76.             default:
  77.                 return this._result.searchResult;
  78.         }
  79.     },
  80.     
  81.     get defaultIndex() 
  82.     {
  83.         if(this._result.defaultIndex == -1 &&
  84.             this._search_result.values != null && this._search_result.values.length > 0)
  85.         {
  86.             return 0;
  87.         }
  88.         return this._result.defaultIndex;
  89.     },
  90.     
  91.     get errorDescription() 
  92.     {
  93.         return this._result.errorDescription;
  94.     },
  95.  
  96.     get matchCount()
  97.     {
  98.         var c = Math.min(this._result.matchCount, this._maxNumResults);
  99.         if(this._search_result.values != null)
  100.         {
  101.             c += this._search_result.values.length;
  102.         }
  103.         return c;    
  104.         //return this._result.matchCount;
  105.     },
  106.     
  107.     getValueAt: function(index) 
  108.     {
  109.         var numResults = Math.min(this._result.matchCount, this._maxNumResults);
  110.         if(index < numResults)
  111.         {
  112.             return this._result.getValueAt(index);
  113.         }
  114.         else
  115.         {
  116.             return this._search_result.values[index - numResults];
  117.         }
  118.     },
  119.     
  120.     getCommentAt: function(index) 
  121.     {
  122.         var numResults = Math.min(this._result.matchCount, this._maxNumResults);
  123.         if(index < numResults)
  124.         {
  125.             return this._result.getCommentAt(index);
  126.         }
  127.         else
  128.         {
  129.             LOG("getCommentAt(" + index + "): " + this._search_result.comments[index - numResults]);
  130.             return this._search_result.comments[index - numResults]
  131.                 .replace(/"/g, '"')
  132.                 .replace(/&/g, '&')
  133.                 .replace(/</g, '<')
  134.                 .replace(/>/g, '>');
  135.         }
  136.     },
  137.     
  138.     getStyleAt: function(index) 
  139.     {
  140.         if(index < Math.min(this._result.matchCount, this._maxNumResults))
  141.         {
  142.             return this._result.getStyleAt(index);
  143.         }
  144.         else
  145.         {
  146.             if(index == 0) //_result might be empty
  147.             {
  148.                 return "suggestfirst smarterwiki-suggest";  // category label on first line of results
  149.             }
  150.             else
  151.             {
  152.                 return "suggesthint smarterwiki-suggest";   // category label on any other line of results
  153.             }
  154.         }
  155.     },
  156.     
  157.     getImageAt: function(index) 
  158.     {
  159.         var numResults = Math.min(this._result.matchCount, this._maxNumResults);
  160.         if(index < numResults)
  161.         {
  162.             return this._result.getImageAt(index);
  163.         }
  164.         else
  165.         {
  166.             return "chrome://smarterwiki/skin/smarterfox-logo-32x32.png";//"moz-anno:favicon:http://www.google.com/favicon.ico";
  167.         }
  168.     },
  169.     
  170.     removeValueAt: function(index, removeFromDb) 
  171.     {
  172.         var numResults = Math.min(this._result.matchCount, this._maxNumResults);
  173.         if(index < numResults)
  174.         {
  175.             return this._result.removeValueAt(index, removeFromDb);
  176.         }
  177.         else
  178.         {
  179.             this._search_result.values.splice(index - numResults, 1);
  180.             this._search_result.comments.splice(index - numResults, 1);
  181.         }
  182.     },
  183.  
  184.     QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIAutoCompleteResult]),
  185. };
  186.  
  187.  
  188. var JSON = Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON);
  189.  
  190.  
  191. function SmarterWikiAutoCompleteSearch() { }
  192.  
  193. SmarterWikiAutoCompleteSearch.prototype = 
  194. {
  195.     classDescription: "Smarter Wikipedia Auto-Complete Search Javascript XPCOM Component",
  196.     classID:          Components.ID("bd8cdff6-4312-43e1-b215-1663e9e8a2e2"),
  197.     contractID:       "@mozilla.org/autocomplete/search;1?name=smarterwiki-search",
  198.     QueryInterface:   XPCOMUtils.generateQI([Components.interfaces.nsIAutoCompleteSearch]),
  199.     startSearch:      function(searchString, searchParam, previousResult, listener)
  200.     {
  201.         this.historyAutoComplete =
  202.                 Components.classes["@mozilla.org/autocomplete/search;1?name=history"]
  203.                 .createInstance(Components.interfaces.nsIAutoCompleteSearch);
  204.  
  205.         var prefManager = Components.classes["@mozilla.org/preferences-service;1"]
  206.                                    .getService(Components.interfaces.nsIPrefBranch);
  207.  
  208.         var _search = this;
  209.         _search._result = null;
  210.         var search_result = {values: null, comments: null};
  211.         var searchAutoCompleteResult = null;
  212.         autoCompleteObserver = 
  213.         {
  214.             onSearchResult: function(search, result)
  215.             {
  216.                 _search._result = result;
  217.                 listener.onSearchResult(_search, new SearchAutoCompleteResult(result, search_result, prefManager.getIntPref("extensions.smarterwiki.num_urlbar_from_history")));
  218.             }
  219.         };
  220.         LOG(previousResult + ": " + previousResult);
  221.         this.historyAutoComplete.startSearch(searchString, searchParam, null, autoCompleteObserver);//previousResult
  222.         
  223.  
  224.         var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance();
  225.         req.open("GET", "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&q=" + encodeURIComponent(searchString));
  226.         //req.setRequestHeader("Referer", "http://smarterfox.com/");
  227.         req.addEventListener("load", function(event)
  228.         {
  229.             LOG("SmarterWikiAutoCompleteSearch got results");
  230.             var data = JSON.decode(req.responseText);
  231.             var values = [];
  232.             var comments = [];
  233.             for(var i in data["responseData"]["results"])
  234.             {
  235.                 values.push(data["responseData"]["results"][i]["unescapedUrl"]);
  236.                 comments.push(data["responseData"]["results"][i]["titleNoFormatting"]);
  237.             }
  238.             search_result.values = values;
  239.             search_result.comments = comments;
  240.             LOG(_search._result);
  241.             LOG("SmarterWikiAutoCompleteSearch got results end");
  242.             if(_search._result != null)
  243.             {
  244.                 listener.onSearchResult(_search, 
  245.                     new SearchAutoCompleteResult(_search._result, search_result, 4)
  246.                 );
  247.             }
  248.             /*
  249.             else
  250.             {
  251.                 listener.onSearchResult(_search, 
  252.                     new SearchAutoCompleteResult(
  253.                         searchString,
  254.                         Components.interfaces.nsIAutoCompleteResult.RESULT_SUCCESS_ONGOING,
  255.                         0,
  256.                         "",
  257.                         values, comments)
  258.                 );
  259.             }
  260.             */
  261.         }, false);
  262.         req.send(null);
  263.         this._req = req;
  264.  
  265.  
  266.         LOG("startSearch: " + searchString);
  267.     },
  268.  
  269.     stopSearch:       function()
  270.     {
  271.         LOG("abort() called");
  272.         this.historyAutoComplete.stopSearch();
  273.         if(this._req)
  274.         {
  275.             this._req.abort();
  276.         }
  277.         LOG("successfully aborted");
  278.     },
  279. };
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286. function NSGetModule(compMgr, fileSpec) 
  287. {
  288.     return XPCOMUtils.generateModule([SmarterWikiAutoCompleteSearch]);
  289. }